<template>
  <div>
    <ul class="list-ul" v-if="list.length > 0">
      <li
        v-for="item of list"
        :key="item.id"
        @click="goPay(item)"
        :class="activeAmount == item.price ? 'active' : ''"
      >
        {{ item.price }} USD
      </li>
      <li class="amountInput">
        <el-input
          v-model="amount"
          type="number"
          :placeholder="$t('amountTips')"
          @change="handleInput"
        ></el-input>
      </li>
    </ul>
    <div class="formStyle">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        label-width="150px"
        label-position="left"
      >
        <el-form-item prop="bank_user_name" :label="$t('name')">
          <el-input
            v-model="ruleForm.bank_user_name"
            :placeholder="$t('nameTips')"
          ></el-input>
        </el-form-item>
        <el-form-item prop="bank_name" :label="$t('BankName')">
          <el-input
            v-model="ruleForm.bank_name"
            :placeholder="$t('bankNameTips')"
          ></el-input>
        </el-form-item>
        <el-form-item prop="card_number" :label="$t('cardNum')">
          <el-input
            v-model="ruleForm.card_number"
            :placeholder="$t('cardNumTips')"
          ></el-input>
        </el-form-item>
        <el-form-item prop="bank_branch" :label="$t('branchName')">
          <el-input
            v-model="ruleForm.bank_branch"
            :placeholder="$t('branchNameTips')"
          ></el-input>
        </el-form-item>
        <el-form-item prop="amount" :label="$t('amount')">
          <el-input
            v-model="ruleForm.amount"
            type="number"
            :placeholder="$t('amountTips')"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="submitBtn">
      <el-button
        type="primary"
        @click="submitForm('ruleForm')"
        v-loading="loading"
        >{{ $t("submit") }}</el-button
      >
    </div>
  </div>
</template>
<script>
import { httpWithdrawSubmit, httpInfo } from "@/http/api";
import { formatMoney } from "@/utils/common";
export default {
  name: "withdrawal",
  data() {
    return {
      value: 0,
      ruleForm: {
        amount: "",
      },
      list: [
        {
          price: 100,
        },
        {
          price: 300,
        },
        {
          price: 500,
        },
        {
          price: 1000,
        },
        {
          price: 2000,
        },
        {
          price: 3000,
        },
        {
          price: 5000,
        },
      ],
      activeAmount: "",
      amount: "",
      rules: {
        bank_user_name: {
          required: true,
          message: this.$t("nameTips"),
          trigger: "change",
        },
        card_number: {
          required: true,
          message: this.$t("cardNumTips"),
          trigger: "change",
        },
        bank_name: {
          required: true,
          message: this.$t("bankNameTips"),
          trigger: "change",
        },
        amount: {
          required: true,
          message: this.$t("amountTips"),
          trigger: "change",
        },
        bank_branch: {
          required: true,
          message: this.$t("branchNameTips"),
          trigger: "change",
        },
      },
      assets_available: "",
      loading: false,
    };
  },
  created() {
    // this.getHttpGoodsList();
    this.getHttpInfo();
  },
  methods: {
    formatMoney,
    handleInput(value) {
      // 如果输入值小于 10，则将其设为 10
      if (value < 10) {
        this.amount = 10;
      } else {
        this.amount = value;
      }
      this.ruleForm.amount = this.amount;
    },
    async getHttpInfo() {
      let res = await httpInfo();
      if (res.code == 0) {
        this.assets_total = res.data?.diamond || 0;
      }
    },
    goPay(item) {
      this.activeAmount = item.price;
      this.ruleForm.amount = item.price;
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (Number(this.ruleForm.amount) > Number(this.assets_total))
            return this.$message.error("Insufficient Balance");
          this.loading = true;
          let params = {
            way: "bank",
            bank_no: this.ruleForm.card_number,
            bank_name: this.ruleForm.bank_name,
            bank_user_name: this.ruleForm.bank_user_name,
            id_number: this.id_number,
            amount: Number(this.ruleForm.amount),
          };
          let res = await httpWithdrawSubmit(params);
          this.loading = false;

          if (res.code == 0) {
            this.$confirm(
              "The review time is 1-3 working days, please be patient",
              "提示",
              {
                confirmButtonText: "ok",
                type: "info",
              }
            )
              .then(() => {
                this.$message({
                  type: "success",
                  message: "Submitted successfully!",
                });
                this.ruleForm = {};
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消删除",
                });
              });
          } else {
            this.$message.success(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.list-ul {
  display: grid;
  grid-template-columns: repeat(auto-fill, 20%);
  justify-content: space-between;
  gap: 10px;
  li {
    width: 100%;
    background: #e6e6e6;
    border-radius: 10px;
    padding: 15px;
    text-align: center;
    box-sizing: border-box;
    cursor: pointer;
  }
  .active {
    background: #efcd2e;
    color: #fff;
  }
}
.formStyle {
  margin-top: 30px;
}
.submitBtn {
  display: flex;
  justify-content: center;
  align-items: center;
  .el-button {
    width: 200px;
    border-radius: 15px;
    background: #dfeaec;
    border: 1px solid #dfeaec;
    color: #333;
    font-size: 18px;
  }
}
</style>
